package com.hinnotek.amoeba.ui.fragment

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.SysOrgAmibaDetailListAdapter
import com.hinnotek.amoeba.bean.old.SysOrgAmibaDetailListBean
import com.hinnotek.amoeba.databinding.FragmentHomeManagerWorkBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.ui.activity.old.StatisticalActivity
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.ManageViewModel
import com.lee.common.base.BaseFragment
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

/**
 * "项目规划"Fragment标签页
 */
@AndroidEntryPoint
class FragmentHomeManagerWork : BaseFragment<FragmentHomeManagerWorkBinding>() {

    private var adapter: SysOrgAmibaDetailListAdapter? = null
    val viewModel by viewModels<ManageViewModel>()
    
    override fun initData() {
        val manager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        mBinding.rlvHomeManager.setLayoutManager(manager)
        mBinding.rlvHomeManager.setAdapter(SysOrgAmibaDetailListAdapter().also { adapter = it })
        adapter!!.setOnItemClickListener { adapter, view, position ->
            val data = adapter.getItem(position) as SysOrgAmibaDetailListBean
            val bundle = Bundle()
            bundle.putInt("orgId", data.orgId)
            IntentUtils.startActivity(mContext, StatisticalActivity::class.java, bundle)
        }
        getSysOrgAmibaDetailList()
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_home_manager_work
    }



    /**
     * app端获取全部阿米巴组织详情带金额接口
     */
    fun getSysOrgAmibaDetailList(){
        val customDialog = CustomDialog(activity, "正在加载...")
        customDialog.show()

        viewModel.getSysOrgAmibaDetailList().observe(this, object : ObserverState<MutableList<SysOrgAmibaDetailListBean>>() {

            override fun onDataChange(data: MutableList<SysOrgAmibaDetailListBean>) {
                customDialog.dismiss()
                mBinding.tvAmoebaTotal.text = "阿米巴总计:" + data.size
                adapter!!.setList(data)
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(activity, msg)
                super.onError(msg, code)
            }

        })
    }



}